home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Games / MoofWars / MoofEncoder / PICTEncode.cp < prev    next >
Encoding:
Text File  |  2000-09-28  |  3.6 KB  |  126 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        PICTEncode.cp
  3.  
  4.     Contains:    
  5.  
  6.     Written by: Timothy Carroll    
  7.  
  8.     Copyright:    Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/1/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 4/2/97                        Timothy Carroll    PICTEncode would double-dispose
  21.                                             of the PICT. I wasn't NULLing out the PICT local
  22.                                             after disposing of it explicitly in the loop.
  23.                                             Spotlight found this.
  24.                 2/24/97        Timothy Carroll        Now explicitly include main.h
  25.                 8/15/96        Timothy Carroll    Initial Release
  26.                 
  27.  
  28. */
  29. #include <Resources.h>
  30.  
  31. #include "Main.h"
  32. #include "TGraphic.h"
  33. #include "PictEncode.h"
  34.  
  35.  
  36. OSStatus PICTEncode (short inputFileResNum, short outputFileResNum)
  37. {
  38.     OSStatus        theErr;
  39.  
  40.     UInt16            numPicts, loop;
  41.  
  42.     // we pass these to GetResInfo so that we can get the actual resource ID.
  43.     SInt16            resID;
  44.     ResType            resType;
  45.     Str255            resName;
  46.  
  47.     // Holds the last value on the resource chain since we change the top resource a lot
  48.     SInt16            saveResNum;
  49.  
  50.     // Temporarily holds the resource so we can get information on it
  51.     Handle             pict = NULL;
  52.     TGraphic         *compiled = NULL;
  53.         
  54.     saveResNum = CurResFile();
  55.  
  56.     UseResFile (inputFileResNum);
  57.  
  58.  
  59.     // First thing is to copy the color table resource to the output.
  60.     theErr = CopyResource (inputFileResNum, outputFileResNum,'clut', kAppColorTableResID);
  61.     FAIL_OSERR (theErr, "\pFailed to copy the color table to the destination file")
  62.  
  63.  
  64.     // get the color table
  65.     gAppColorTable = GetCTable( kAppColorTableResID );
  66.     FAIL_NIL (gAppColorTable, "\pFailed to open the color table")
  67.  
  68.     // determine the number of PICT resources
  69.     numPicts = Count1Resources( 'PICT' );
  70.  
  71.     
  72.     // get each one,
  73.     for( loop = 1; loop <= numPicts; loop++ )
  74.     {
  75.         // load the pict
  76.         UseResFile (inputFileResNum);
  77.                     
  78.         // we'll get the pict first so that we can get the information out of it.
  79.         pict = Get1IndResource( 'PICT', loop );
  80.         theErr = ResError();
  81.         FAIL_NIL (pict, "\pFailed to load the picture")
  82.         FAIL_OSERR (theErr, "\pFailed to load the picture")
  83.             
  84.         // determine its id and name
  85.         GetResInfo( pict, &resID, &resType, resName );
  86.         theErr = ResError();
  87.         FAIL_OSERR( theErr, "\pFailed to get info on the resource")
  88.         
  89.         ReleaseResource (pict);
  90.         pict = NULL; // We're done with this PICT, NULL it out so we don't double dispose
  91.         
  92.         
  93.         // Okay, we know the PICT's resID, build the compiled graphic and write it to the output file.
  94.         compiled = TGraphic::NewGraphic (resID);
  95.         FAIL_NIL (compiled, "\pFailed to compile the TGraphic")
  96.         
  97.         UseResFile (outputFileResNum);
  98.         
  99.         compiled->WriteToGraphicResource();
  100.         compiled->WriteToPICTResource();
  101.         compiled->DisposeReference();
  102.         
  103.         compiled = NULL; // Make sure we don't double dispose of the compiled sprite
  104.  
  105.     }
  106.     // restore the previous port and device
  107.     
  108.     goto cleanup;
  109.     
  110. error:
  111.     
  112.     if (theErr == noErr)
  113.         theErr = paramErr;
  114. cleanup:
  115.     
  116.     UseResFile (saveResNum);
  117.     
  118.     if (pict)
  119.         ReleaseResource (pict);
  120.     if (compiled)
  121.         compiled->DisposeReference();
  122.     if (gAppColorTable != NULL)
  123.         DisposeCTable (gAppColorTable);
  124.     gAppColorTable = NULL;
  125.     return theErr;
  126. }